package com.kitty.game.equip.model;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.FieldValuePosConst;
import com.kitty.game.attribute.AttrService;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.utils.NumberUtil;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import java.util.LinkedHashMap;

@Data
@Slf4j
@JsonIgnoreProperties(ignoreUnknown = true)
public class RoleEquip {

    /**
     * 装备id
     */
    private int id;

    /**
     * 佩戴位置
     */
    private short position;

    /**
     * 角色id
     */
    private int roleId;

    /**
     * 物品类型
     */
    private String type;

    /**
     * 装备名称
     */
    private String name;

    private byte qilingstate ;

    /**
     * 属性map
     */
    private LinkedHashMap<Short, RoleEquipField> fields = new LinkedHashMap<>();

    /**
     * 魂器属性map
     */
    private LinkedHashMap<Short, RoleHunQiField> hunQiFields = new LinkedHashMap<>();

    /**
     * 改造进度
     */
    private int gaizaoJindu;

    /**
     * 天书使用状态
     */
    private boolean bookUse;

    /**
     * 完美度
     */
    private int perfect;

    public void setPosition(int position) {
        this.position = (short) position;
    }


    /**
     * 取首饰强化等级
     */
    public short queryStrengthenLevel() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 459);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()).shortValue();
        } else {
            return 0;
        }
    }

    /**
     * 设置首饰强化等级
     */
    public void alterStrengthenLevel(int level) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 459);
        if (fiedValue != null) {
            fiedValue.setValue(level);
        } else {
            fiedValue = new FiedValue(459, 2, level);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 取首饰强化进度
     */
    public short queryStrengthenProgress() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 461);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()).shortValue();
        } else {
            return 1;
        }
    }

    /**
     * 设置首饰强化进度
     */
    public void alterStrengthenProgress(int progress) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 461);
        if (fiedValue != null) {
            fiedValue.setValue(progress);
        } else {
            fiedValue = new FiedValue(461, 2, progress);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 判断物品是否是永久限制
     */
    @JsonIgnore
    public boolean isEverLimit() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 270);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()) == 2;
        } else {
            return false;
        }
    }

    /**
     * 判断物品是否是限制
     */
    @JsonIgnore
    public boolean isLimit() {
        int currTime = -Math.toIntExact(System.currentTimeMillis() / 1000);
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 270);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()) < currTime || isEverLimit();
        } else {
            return false;
        }
    }

    /**
     * 取物品的叠加数量
     */
    public short queryNum() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 203);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()).shortValue();
        } else {
            return 1;
        }
    }

    /**
     * 取物品的颜色
     */
    public String queryColor() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 209);
        if (fiedValue != null) {
            return fiedValue.getValue().toString();
        } else {
            return "白色";
        }
    }

    /**
     * 设置颜色
     */
    public void alterColor(String color) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 209);
        if (fiedValue != null) {
            fiedValue.setValue(color);
        } else {
            fiedValue = new FiedValue(209, 4, color);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 设置套装属性生效 1生效 0不生效
     */
    public void alterSuitEnabled(int enabled) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 269);
        if (fiedValue != null) {
            fiedValue.setValue(enabled);
        } else {
            fiedValue = new FiedValue(269, enabled);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 设置套装相性
     */
    public void alterSuitPolar(int polar) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 268);
        if (fiedValue != null) {
            fiedValue.setValue(polar);
        } else {
            fiedValue = new FiedValue(268, polar);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 取装备相性
     */
    public short querySuitPolar() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 268);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()).shortValue();
        } else {
            return 0;
        }
    }


    /**
     * 添加数量
     */
    public void alterNum(int num) {
        String equipType = this.getType();
        if (equipType.equals("装备") || equipType.equals("高级首饰") || equipType.equals("法宝") || equipType.equals("超级黑水晶·")) {
            try {
                throw new Exception("改变装备数量");
            } catch (Exception e) {
             //   LoggerFunction.BUG.getLogger().error("BUG修改装备数量,角色ID：[{}],物品名：[{}]，原有[{}],现有[{}]，调用信息：[{}]", roleId, this.getName(), getEquipValue(FieldValuePosConst.NUM), num, Lang.getStackTrace(e));
            }
        }

        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 203);
        if (fiedValue != null) {
            fiedValue.setValue(num);
        } else {
            fiedValue = new FiedValue(203, 2, num);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 取出装备的佩戴部位
     * isCrystal Todo 是不是黑水晶
     */
    public byte queryEquipPos(boolean isCrystal) {
        int fieldPos = isCrystal ? 248 : 202;
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) fieldPos);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()).byteValue();
        } else {
            return 0;
        }
    }

    /**
     * 取物品等级
     */
    public short queryLevel() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get(FieldValuePosConst.LEVEL);
        if (fiedValue != null) {
            return NumberUtil.shortValue(fiedValue.getValue());
        } else {
            return 1;
        }
    }

    /**
     * 取改造等级
     */
    public short queryUpgradeLevel() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 208);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()).shortValue();
        } else {
            return 0;
        }
    }

    /**
     * 设置改造等级
     */
    public void alterUpgradeLevel(int upgradeLevel) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 208);
        if (fiedValue != null) {
            fiedValue.setValue(upgradeLevel);
        } else {
            fiedValue = new FiedValue(208, 3, upgradeLevel);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 设置进化等级
     */
    public void alterEvolveLevel(int evolveLevel) {
        alterLevel(evolveLevel);
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 205);
        if (fiedValue != null) {
            fiedValue.setValue(evolveLevel);
        } else {
            fiedValue = new FiedValue(205, 3, evolveLevel);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 设置物品等级
     */
    public void alterLevel(int level) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get(FieldValuePosConst.LEVEL);
        if (fiedValue != null) {
            fiedValue.setValue(level);
        } else {
            fiedValue = new FiedValue(31, 3, level);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }


    /**
     * 取进化等级
     */
    public int queryEvolveLevel() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 205);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue());
        } else {
            return 0;
        }
    }


    /**
     * 取改造进度
     */
    public int queryUpgradeProgress() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 198);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue());
        } else {
            return 0;
        }
    }


    /**
     * 设置改造等级
     */
    public void alterUpgradeProgress(int upgradeProgress) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 198);
        if (fiedValue != null) {
            fiedValue.setValue(upgradeProgress);
        } else {
            fiedValue = new FiedValue(198, 3, upgradeProgress);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 添加限制时间
     */
    public void addLimitTime(int limitNum) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 270);
        if (fiedValue != null && limitNum > 0 && !this.isEverLimit()) {
            int value = SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue());
            int currTime = new Long(System.currentTimeMillis() / 1000).intValue();
            if (value == 0) {
                value = currTime;
                fiedValue.setValue(-(value + 864000 * limitNum));// 增加限制天数
            } else {
                if (-value + 864000 * limitNum >= currTime + 864000 * 6) {
                    fiedValue.setValue(-(currTime + 864000 * 6));// 增加限制天数
                } else {
                    fiedValue.setValue(value - 864000 * limitNum);// 增加限制天数
                }
            }
        }
    }


    /**
     * 取法宝当前灵气
     */
    public int queryArtifactNimbus() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 118);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue());
        } else {
            return 0;
        }
    }

    /**
     * 设置法宝最大灵气
     */
    public void addArtifactNimbus(int nimbus) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 118);
        if (fiedValue != null) {
            fiedValue.setValue(nimbus);
        } else {
            fiedValue = new FiedValue(118, 3, nimbus);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }
    /**
     * 设置法宝亲密度
     * @param intimacy
     */
    public void addArtifactIntimacy(int intimacy) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 63);
        if (fiedValue != null) {
            fiedValue.setValue(intimacy + Integer.parseInt(fiedValue.getValue().toString()));
        } else {
            fiedValue = new FiedValue(63, 3, intimacy);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 设置首饰转换次数
     */
    public void addTransformCount(int num) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 451);
        if (fiedValue != null) {
            fiedValue.setValue(SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue()) + num);
        } else {
            fiedValue = new FiedValue(451, num);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 设置首饰转换时间限制
     */
    public void addTransformTime(int time) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 452);
        if (fiedValue != null) {
            fiedValue.setValue(time);
        } else {
            fiedValue = new FiedValue(452, time);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 取首饰转换时间限制
     */
    public int queryTransformTime() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 452);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue());
        } else {
            return 0;
        }
    }

    /**
     * 取首饰转换次数
     */
    public int queryTransformCount() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 451);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue());
        } else {
            return 0;
        }
    }


    public void alterId(int id) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 84);
        if (fiedValue != null) {
            fiedValue.setValue(id);
        } else {
            fiedValue = new FiedValue(84, id);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    public void alterGid(String gid) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 306);
        if (fiedValue != null) {
            fiedValue.setValue(gid);
        } else {
            fiedValue = new FiedValue(306, gid);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

    /**
     * 取出售价格
     */
    public int queryPrice() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 207);
        if (fiedValue != null) {
            return NumberUtil.intValue(fiedValue.getValue());
        } else {
            return 500;
        }
    }

    /**
     * 取进化次数
     */
    public int queryEvolveCount() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 881);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue());
        } else {
            return 0;
        }
    }

    /**
     * 设置进化次数
     */
    public void alterEvolveCount(int count) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get((short) 881);
        if (fiedValue != null) {
            fiedValue.setValue(count);
        } else {
            fiedValue = new FiedValue(881, count);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }


    /**
     * 判断是否是贵重物品
     */
    @JsonIgnore
    public boolean isExpensive() {
        /**如果是10级以上法宝*/
        if (type.equals("法宝") && queryLevel() >= 10) {
            return true;
        }
        /**如果是改造6以上的装备*/
        if (type.equals("装备") && queryUpgradeLevel() >= 6) {
            return true;
        }
        /**如果是贵重首饰*/ //Todo 贵重首饰的计算有点麻烦 先不写了
        if (type.equals("高级首饰")) {
            return true;
        }

        return false;
    }

    /**
     * 是否法宝
     *
     * @return
     */
    @JsonIgnore
    public boolean isArtifact() {
        return "法宝".equals(getType());
    }
    /**
     * 是否法宝
     *
     * @return
     */
    @JsonIgnore
    public boolean isHunQi() {
        return "魂器".equals(getType());
    }

    /**
     * 取法宝相性
     */
    public int queryArtifactPolar() {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get(FieldValuePosConst.ARTIFACT_POLAR);
        if (fiedValue != null) {
            return SpringUtils.getBean(AttrService.class).getValue(fiedValue.getValue());
        } else {
            return 1;
        }
    }

    /**
     * 取装备属性
     */
    @JsonIgnore
    public int getEquipValue(short key) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get(key);
        if (fiedValue != null) {
            return NumberUtil.intValue(fiedValue.getValue());
        } else {
            return 0;
        }
    }

    /**
     * 取装备属性
     */
    @JsonIgnore
    public <T> T getEquipValueObject(short key) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get(key);
        if (fiedValue != null) {
            return (T) fiedValue.getValue();
        } else {
            return null;
        }
    }

    /**
     * 设置装备属性
     */
    @JsonIgnore
    public void setEquipValue(short key, Object value) {
        FiedValue fiedValue = this.getFields().get((short) 1).getField().get(key);
        if (fiedValue != null) {
            fiedValue.setValue(value);
        } else {
            fiedValue = new FiedValue(key, value);
            this.getFields().get((short) 1).getField().put(fiedValue.getType(), fiedValue);
        }
    }

}
